import MainBoard 1.0
import QtQuick.Window 2.1
import QtQuick 2.0
import QtQuick.Dialogs 1.1

Window {
    id: player_father
    width: 1280
    height: 800

    property string server_ip: control_widget.server_address
    property string server_port: control_widget.server_port

    signal maximized_window();
    signal minimized_window();

    function show_right_pop_up_menu(mouseX, mouseY) {
        if (mouseX >= player_widget.width - right_pop_menu.width)
            right_pop_menu.x = player_widget.width - right_pop_menu.width + player_father.x
        else
            right_pop_menu.x = mouseX + player_father.x

        if (mouseY >= player_widget.height - right_pop_menu.height)
            right_pop_menu.y = player_widget.height - right_pop_menu.height + player_father.y
        else
            right_pop_menu.y = mouseY + player_father.y
    }

    Rectangle {
        id: player
        anchors.fill: parent

        property int left_margin: 17
        property int right_margin: 17
        property int list_margin: 0
        property int top_margin: 10

        property real process_now_pos
        property real process_length

        color: "#4e6dae"

        Rectangle {
            id: main_widget
            width: parent.width
            height: parent.height
            anchors.fill: parent
            color: parent.color
        }

        DropArea {
            anchors.fill: parent
            keys: ["text/plain"]
            onEntered: {
            }
            onExited: {
            }
            onDropped: {
                if (drop.hasText) {
                    if (drop.proposedAction == Qt.MoveAction || drop.proposedAction == Qt.CopyAction) {
                        player_widget.movieFilePath = drop.text
                        console.log(drop.text)
                        drop.acceptProposedAction()
                        control_widget.exchange_play_and_pause_btn()
                        control_widget.play_btn_clicked()
                    }
                }
            }
        }

        Window {
            id: player_widget_father
            flags: Qt.FramelessWindowHint | Qt.Popup
            x: player_father.x + player.left_margin
            y: player_father.y + player.top_margin
            width: player.width - player.left_margin - player.right_margin - list_video_menu.width - player.list_margin
            height: player.height - player.top_margin - control_widget.height - 10
            visible: false
            MainBoard {
                id: player_widget
                anchors.fill: parent
                movieFilePath: "juejiang.mp4"
                onProcess_value_changed: {
                    console.log(processValue / processLength)
                    control_widget.set_process_percent(processValue, processLength)
                }

                onProcess_finished: {
                    player_widget_father.visible = false
                }

                MouseArea {
                    anchors.fill: parent
                    acceptedButtons: Qt.LeftButton | Qt.RightButton
                    onDoubleClicked: console.log("double click")
                    hoverEnabled: true
                    onPressed: {
                        console.log("someting pressed")
                        control_widget.disvisible_setting_pop_up_menu()
                        control_widget.update()
                        if (mouse.button == Qt.RightButton) {
                            right_pop_menu.visible = true
                            show_right_pop_up_menu(mouseX, mouseY)
                        } else {
                            right_pop_menu.visible = false
                        }
                    }
                }
            }
        }

        MouseArea {
            id: pop_up_menu_area
            acceptedButtons: Qt.LeftButton | Qt.RightButton
            anchors.fill: parent
            onDoubleClicked: console.log("double click")
            hoverEnabled: true
            onPressed: {
                console.log("someting pressed")
                control_widget.disvisible_setting_pop_up_menu()
                control_widget.update()
                if (mouse.button == Qt.RightButton) {
                    right_pop_menu.visible = true
                    show_right_pop_up_menu(mouseX, mouseY)
                } else {
                    right_pop_menu.visible = false
                }
            }
        }

        ListModel {
            id: right_pop_menu_context
            ListElement {
                name: "prev media"
            }
            ListElement {
                name: "next media"
            }
            ListElement {
                name: "config server"
            }
            ListElement {
                name: "close"
            }
        }

        PopMenu {
            id: right_pop_menu
            element_height: 20
            pop_menu_model: right_pop_menu_context
            visible: false
            context_count: right_pop_menu_context.count
            onMenu_item_clicked: {
                console.log("right pop menu " + index)
                if (index == 0) {
                    /* prev todo */
                } else if (index == 1) {
                    /* next todo */
                } else if (index == 2) {
                    /* config server */
                    control_widget.set_config_server_visible(true)
                } else if (index == 3) {
                    Qt.quit()
                }
            }
        }

        ListWidget {
            id: list_video_menu
            width: 0
            height: player.height - player.top_margin - 10
            x: player_father.x + player.width - width - player.right_margin
            y: player_father.y + player.top_margin
            onVisibleChanged: {
                if (visible) {
                    player.list_margin = 17
                    list_video_menu.width = 200
                    update()
                } else {
                    player.list_margin = 0
                    list_video_menu.width = 0
                    update()
                }
            }
            Component.onCompleted: {
                visible = false
            }
        }

        ControlWidget {
            id: control_widget
            width: player.width - list_video_menu.width - player.list_margin
            height: 87
            y: parent.height - height
            color: parent.color
            left_margin: parent.left_margin
            right_margin: parent.right_margin

            offsetx: player_father.x + x
            offsety: player_father.y + y

            onPlay_btn_clicked: {
                console.log("play button clicked")
                player_widget_father.visible = true
                player_widget.play_pause_btn_click_slots()
            }
            onPause_btn_clicked: {
                console.log("pause button clicked")
                player_widget.play_pause_btn_click_slots()
            }
            onStop_btn_clicked: {
                console.log("stop button clicked")
                player_widget_father.visible = false
                player_widget.stop_btn_click_slots()
            }
            onPrev_btn_clicked: {
                console.log("prev button clicked")
            }
            onWidget_clicked: {
                disvisible_setting_pop_up_menu()
                update()
            }
            onList_btn_clicked: {
                list_video_menu.visible = !list_video_menu.visible
                update()
            }
            onProcess_slider_value_changed: {
                player_widget.process_slider_valuechanged_slots(value)
            }
            onVolume_slider_value_changed: {
                player_widget.volume_slider_valuechanged_slots(value)
            }
            onMute_btn_clicked: {
                player_widget.mute_btn_click_slots()
            }
        }

        FileDialog {
            id: open_file_dialog
            title: "Please choose a file"
            modality: Qt.NonModal
            onAccepted: {
                console.log(open_file_dialog.fileUrls)
                player_widget.movieFilePath = open_file_dialog.fileUrl.toString()
//                list_video_menu.insert_element(1, -1, -1, {name: player_widget.movieFilePath})
                visible = false
            }
            onRejected: {
                console.log("canceled")
                visible = false
            }
            Component.onCompleted: visible = false
        }
    }
}
